﻿# -*- coding: utf-8 -*-
'''eof
name:贷款及其他账户逾期总数
code:CreditOperationOverdueCnt_2D
tableName:
columnName:
groups:个人征信
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:
eof'''

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None or table == "":
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return [{}]
        elif table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)


def value(x,y):
     null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
     if x.has_key(y) and isfloat(x[y])==True and x[y] not in null_type_list:
         return float(x[y])
     else:
         return 0


def IC9_CreditOperationOverdueCnt():
    data = IC_DSJ_2D['data']
    account=[]
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        table1=getTable(data,"PCO",1)
        if table1 is None:
            return u"缺失值"
        elif table1==[{}]:
            return u"缺失值"
        else:
            #逾期（透支）汇总信息段
            table2=getTable(table1[0],"PC02",0)
            if table2 is None:
                return u"缺失值"
            elif table2==[{}]:
                return u"缺失值"
            else:
                #逾期（透支）汇总信息段-或有
                PC02D=getTable(table2[0],"PC02D",0)
                if PC02D in [ None,[{}]]:
                    return u"缺失值"
                else:
                    for i in PC02D:
                        try:
                            PC02DH = getTable(i, 'PC02DH', 1)
                            if PC02DH is None:
                                account.append(None)
                            else:
                                #账户数
                                PC02DS02=PC02DH[0].get('PC02DS02')
                                if PC02DS02 not in null_type_list and isfloat(PC02DS02)==True:
                                    account.append(float(PC02DS02))
                                else:
                                    account.append(None)
                        except:
                            account.append(None)
                            continue
                factor2NotNone = filter(lambda x: x is not None, account)
                if len(set(factor2NotNone))==0:
                    return u'缺失值'
                else:
                     return sum(factor2NotNone)
    except:
        return u'缺失值'

result = IC9_CreditOperationOverdueCnt()














